package com.blog.controller;

import com.alibaba.fastjson.JSONObject;
import com.blog.pojo.User;
import com.blog.utils.BlogImageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Copyright (C), 2022
 * Author: 苏大大
 * Date: 2022-06-12 21:27
 * FileName: BlogImageController
 * Description: 博客图片处理
 */
@RestController
@RequestMapping("blogImg")
@Slf4j
public class BlogImageController extends BaseController{
    @PostMapping("/UploadContentImg/{uid}")
    public String UploadConnetImg(MultipartFile file, HttpSession session,@PathVariable("uid") Long uid) throws IOException {
        JSONObject object= new JSONObject() ;
        log.info( "检查用户是否登录");
        User user=checkUser(uid);
        if (user != null) {
            log.info("获取用户信息,用户id为{}",user.getUid());
        }else{
            object.put("flag",false);
            return object.toJSONString();
        }
        //判断是否上传了头像
        String filepath= BlogImageUtils.uploadBlog(file);

        object.put("url",filepath);
        object.put("flag",true);
        return object.toJSONString();
    }
    @PostMapping("/UploadFirstImg/{uid}")
    public String updateAvatarById(MultipartFile file,@PathVariable("uid") Long uid) throws IOException {
        JSONObject object= new JSONObject() ;
        log.info( "检查用户是否登录");
        User user=checkUser(uid);
        if (user != null) {
            log.info("获取用户信息,用户id为{}",user.getUid());
        }else{
            object.put("flag",false);
            return object.toJSONString();
        }
        //判断是否上传了头像
        String filepath= BlogImageUtils.uploadBlogFirst(file);

        object.put("url",filepath);
        object.put("flag",true);
        return object.toJSONString();
    }
}
